<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //时间格式化处理
            String.prototype.formatTime=function formatTime(template){
				//1.this--str     2.tempalte
                if(template===undefined){
                    template="{0}年{1}月{2}日 {3}:{4}"
                }
                //1.将字符串中的日期取出来
                let arr=this.match(/\d+/g);
                //console.log(arr);//['2021', '6', '6', '5', '5', '5']

                template=template.replace(/\{(\d+)\}/g,(value,$1)=>{
                    //console.log($1) 0 1 2 3 4 
                    let item=arr[$1]//arr[0]--2021
                    if(item.length<2){
                        item="0"+item;
                    }
                    return item; 
                })

                return template;
			}
			let str="2021/6/6 5:5:5";
			console.log(str.formatTime());//什么都不传
			console.log(str.formatTime("{0}年{1}月{2}日 {3}:{4}:{5}"));
			console.log(str.formatTime("{1}月{2}日 {3}:{4}"));
			console.log(str.formatTime("{0}年{1}月{2}日"));
			console.log(str.formatTime("{0}-{1}-{2} {3}:{4}"));
    </script>
</body>
</html>